package com.atguigu.tingshu.order.service.impl;

import com.atguigu.tingshu.account.AccountFeignClient;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.order.OrderDetail;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.mapper.OrderInfoMapper;
import com.atguigu.tingshu.order.service.AccountPayBizService;
import com.atguigu.tingshu.order.service.OrderDetailService;
import com.atguigu.tingshu.user.client.UserFeignClient;
import com.atguigu.tingshu.vo.account.AccountDeductVo;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author NJG
 * @version 1.0
 * @date 2025/8/5 17:07
 * @description
 */
@Slf4j
@Service
public class AccountPayBizServiceImpl implements AccountPayBizService {

    @Autowired
    private AccountFeignClient accountFeignClient;
    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private UserFeignClient userFeignClient;



    @GlobalTransactional(rollbackFor = Exception.class)
    public void extracted(OrderInfo orderInfo) {
        //4.1 远程调用账户服务扣减账户余额
        //4.1.1 封装扣减账户VO对象
        AccountDeductVo accountDeductVo = new AccountDeductVo();
        accountDeductVo.setOrderNo(orderInfo.getOrderNo());
        accountDeductVo.setUserId(orderInfo.getUserId());
        accountDeductVo.setAmount(orderInfo.getOrderAmount());
        accountDeductVo.setContent(orderInfo.getOrderTitle());
        //4.1.2 远程调用扣减账户余额
        Result result = accountFeignClient.checkAndDeduct(accountDeductVo);
        //4.1.3 判断远程调用结果业务状态码是否为200
        if (result.getCode().intValue() != 200) {
            throw new GuiguException(result.getCode(), result.getMessage());
        }
        //4.2 如果扣减余额成功，修改订单状态改为：已支付
        orderInfo.setOrderStatus(SystemConstant.ORDER_STATUS_PAID);
        orderInfoMapper.updateById(orderInfo);

        //4.3 远程调用用户服务虚拟物品发货
        //4.3.1 构建虚拟发货VO对象
        UserPaidRecordVo userPaidRecordVo = new UserPaidRecordVo();
        userPaidRecordVo.setOrderNo(orderInfo.getOrderNo());
        userPaidRecordVo.setUserId(orderInfo.getUserId());
        userPaidRecordVo.setItemType(orderInfo.getItemType());
        //根据订单编号查询订单明细获取购买项目ID列表
        List<OrderDetail> orderDetailList = orderDetailService.list(
                new LambdaQueryWrapper<OrderDetail>()
                        .eq(OrderDetail::getOrderId, orderInfo.getId())
                        .select(OrderDetail::getItemId)
        );
        List<Long> itemIdList = orderDetailList.stream()
                .map(OrderDetail::getItemId).collect(Collectors.toList());
        userPaidRecordVo.setItemIdList(itemIdList);

        //4.3.2 远程调用用户服务,发放用户权益
        result = userFeignClient.savePaidRecord(userPaidRecordVo);

        //4.3.3 判断远程调用结果业务状态码是否为200
        if (result.getCode().intValue() != 200) {
            throw new GuiguException(result.getCode(), result.getMessage());
        }
        //模拟发生异常
        /*if (orderInfo.getId() % 2 == 0) {
            int i = 1 / 0;
        }*/
    }
}
